package de.koller.oceanFramework.thread.buffer;

import java.util.LinkedList;
import java.util.List;

public class Buffer<T> {

	private final List<T> buffer = new LinkedList<>();
	
	public synchronized T pop() {
		while( buffer.isEmpty() )
			try { wait(); } catch( InterruptedException ex ) {}
		
		T content = buffer.remove( 0 );
		notifyAll();
		
		return content;
	}

	public synchronized void push( T data ) {
		buffer.add( data );
		if( buffer.size() > 100 )
			System.err.println( "Warning: Buffer bigger than " + buffer.size() );
		notifyAll();
	}
	
	public synchronized boolean isEmpty() {
		return buffer.isEmpty();
	}
	
}

